//
//  FeatureView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/21.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit
/// 新特性界面
class FeatureView: UIView {
    
    private let GXLNewFeatureImageCount = 4

   override   init(frame: CGRect) {
      super.init(frame: UIScreen.mainScreen().bounds)
    
       setupUI()
    
    }

 required init?(coder aDecoder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
 }
    
// MARK: - 私有属性
    
    private lazy var pageContor = UIPageControl()

}
// MARK: - 设置界面
extension FeatureView{
    
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        //使用UIScrollView
        
        let scrollView = UIScrollView(frame: bounds)
        
        addSubview(scrollView)
        
        print(scrollView.frame)
        
       
        
        //添加图像
        
        for i in 0..<GXLNewFeatureImageCount {
            
   
            
           
            
            let iv = UIImageView(image: UIImage(named: "new_feature_1"))
            
         
            
            scrollView.addSubview(iv)
            
            //设置frame
            
            iv.frame = CGRectMake(CGFloat(i)*bounds.width, 0, bounds.width, bounds.height)
            
           
            
         
        }
        
        //设置contentsize
        
        scrollView.contentSize = CGSizeMake(CGFloat(GXLNewFeatureImageCount+1)*bounds.width, 0)
        
        //设置其他属性
        
        scrollView.bounces = false
        
        scrollView.showsHorizontalScrollIndicator = false
        
        scrollView.pagingEnabled = true
        
        //设置分页控件
        
        addSubview(pageContor)
        
        pageContor.hidesForSinglePage = true
        
        pageContor.numberOfPages = GXLNewFeatureImageCount
        
        pageContor.currentPage = 0
        pageContor.pageIndicatorTintColor = UIColor.blackColor()
        pageContor.currentPageIndicatorTintColor = GXLapplearceColor
        //禁用交互
        
        pageContor.userInteractionEnabled = false
        
        //自动布局
        
        pageContor.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            
            make.bottom.equalTo(self).offset(-80)
            
        }
        
        scrollView.delegate = self
        
    }
    
    
}

// MARK: - UIScrollViewDelegate
extension FeatureView: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 1. 计算当前所在页
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        // 2. 如果是最后一页，销毁当前视图
        if page == GXLNewFeatureImageCount {
            removeFromSuperview()
        }
    }
    
    // 只要滚动视图发生滚动就会被调用
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pageContor.currentPage = page
        
        // 如果最后一页，隐藏分页控件
        pageContor.hidden = (page == GXLNewFeatureImageCount)
    }
}

